<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 10] 10.4 External Program Execution</TITLE>
<META NAME="author" CONTENT="Mark Grand and Jonathan Knudsen">
<META NAME="date" CONTENT="Fri Aug  8 16:19:28 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Java Fundamental Classes Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Fundamental Classes Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch10_03.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 10<br>Accessing the Environment</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch10_05.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JFC-CH-10-SECT-4">10.4 External Program Execution</A></h2>

<P CLASS=para>
The <tt CLASS=literal>Runtime.exec()</tt> method allows 
a Java program to run an external program. There are four forms of the 
<tt CLASS=literal>exec()</tt> method, but they all 
expect to receive command-line-style information about the program to be 
run. The simplest <tt CLASS=literal>exec()</tt> method 
takes a single <tt CLASS=literal>String</tt> argument 
that contains the name of the program and its arguments. For example, to 
print a file named <i CLASS=filename>foo.ps</i> in 
a Windows environment, you can use the following: 

<DIV CLASS=screen>
<P>
<PRE>
getRuntime().exec("copy foo.ps lpt1:");
</PRE>
</DIV>

<P CLASS=para>
Another form of <tt CLASS=literal>exec()</tt> takes an array of <tt CLASS=literal>String</tt>
objects as its argument. The first element of the array is the name of
the program to run; the remaining array elements are arguments to the
program. The other two forms of <tt CLASS=literal>exec()</tt> take a
second argument that specifies the environment variables that are
available to the program. The second argument should be an array of
strings that are all of the form
<tt CLASS=replaceable><i>name</i></tt> =<tt CLASS=replaceable><i>value</i></tt>.

<P CLASS=para>
The external program started by <tt CLASS=literal>exec()</tt> is run
asynchronously from the Java program that started it. All forms of the
<tt CLASS=literal>exec()</tt> method return immediately; they return a
reference to a <tt CLASS=literal>Process</tt> object that you can use to
communicate with the external program. The three standard I/O streams
for the external program can be accessed by calling the
<tt CLASS=literal>getInputStream()</tt>,
<tt CLASS=literal>getOutputStream()</tt>, and
<tt CLASS=literal>getErrorStream()</tt> methods of the
<tt CLASS=literal>Process</tt> object.  If you want to wait for the
external program to complete, you can call the
<tt CLASS=literal>waitFor()</tt> method. This method returns the
program's exit code. The <tt CLASS=literal>exitValue()</tt> method
returns the program's exit code if it is called after the
program has completed. If it is called before the program completes,
it throws an <tt CLASS=literal>IllegalThreadStateException</tt>.  You can
kill the external program by calling the <tt CLASS=literal>destroy()</tt>
method.

<P CLASS=para>
If a program is running in a browser or other environment that has a
<tt CLASS=literal>SecurityManager</tt> installed, it may be denied the
ability to execute external programs.

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch10_03.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch10_05.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Environment Variables</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Garbage Collection</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
